package com.bci.pwtz.controller.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.remote.BaseRequest;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.resp.CompanyMessageListResp;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.CompanyMessage;
import com.bci.pwtz.mysql.model.CompanyUserAudit;
import com.bci.pwtz.service.CompanyMessageService;
import com.bci.pwtz.service.CompanyUserAuditService;
@Controller
@RequestMapping("/api/companymessage")
public class CompanyMessageAppController extends AbstractController
{
@Autowired
CompanyMessageService companyMessageService;
@Autowired
CompanyUserAuditService companyUserAuditService;

@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public CompanyMessageListResp getCompanyMessages(HttpServletRequest request){
    BaseRequest companyMessageReq=null;
    CompanyMessageListResp companyMessageResp=new CompanyMessageListResp();
    List<CompanyMessage> list=null;
    try{
        companyMessageReq=getJsonToObj(request,BaseRequest.class);
        if(companyMessageReq==null){
            companyMessageResp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            companyMessageResp.setReturnMsg("请求为空错误");
            return companyMessageResp;
        }
        if(companyMessageReq.getUserId()==null){
            companyMessageResp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            companyMessageResp.setReturnMsg("用户id为空错误");
            return companyMessageResp;
        }
        if(companyMessageReq.getSecretKey()==null){
            companyMessageResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            companyMessageResp.setReturnMsg("秘钥为空");
            return companyMessageResp;
        }
        if(companyMessageReq.getTimestamp()==null){
            companyMessageResp.setReturnCode(ReturnCode.OTHER_ERROR);
            companyMessageResp.setReturnMsg("时间戳为空错误");
            return companyMessageResp;
        }
        if(!validateSecretKey(companyMessageReq.getSecretKey(),companyMessageReq.getTimestamp())){
            companyMessageResp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            companyMessageResp.setReturnMsg("秘钥错误");
            companyMessageResp.setTimestamp(companyMessageReq.getTimestamp());
            return companyMessageResp;
        }
        if(StringUtils.isBlank(companyMessageReq.getToken())){
            companyMessageResp.setReturnCode(ReturnCode.TOKEN_INVALID);
            companyMessageResp.setReturnMsg("票据为空");
            companyMessageResp.setTimestamp(companyMessageReq.getTimestamp());
            return companyMessageResp;
        }
        if(!validateToken(companyMessageReq.getToken(), companyMessageReq.getUserId())){
            companyMessageResp.setReturnCode(ReturnCode.TOKEN_INVALID);
            companyMessageResp.setReturnMsg("票据无效");
            companyMessageResp.setTimestamp(companyMessageReq.getTimestamp());
            return companyMessageResp;
        }
        list=companyMessageService.findCompanyMessageList(companyMessageReq.getUserId());
        if(list !=null){
            companyMessageResp.setReturnCode(ReturnCode.SUCCESS);
            companyMessageResp.setReturnMsg("查询公司信息列表成功");
            for(CompanyMessage cm:list){
                if(cm.getReadStatus()==1){
                    cm.setAppReadTime(new Date());
                }
            }
            companyMessageResp.setObjs(list);
            companyMessageResp.setTimestamp(companyMessageReq.getTimestamp());
            List<CompanyUserAudit> alist=new ArrayList<CompanyUserAudit>();
            for(int i=0; i<list.size();i++){
                list.get(i).setAppStatus(2);
                if(list.get(i).getReadStatus()==1){
                    list.get(i).setReadStatus(2);
                }
                companyMessageService.update(list.get(i));
                if(list.get(i).getMessageType()==2&&list.get(i).getAuditId()>0){
                   CompanyUserAudit audit= companyUserAuditService.load(list.get(i).getAuditId());
                   alist.add(audit);
                }
            }
            companyMessageResp.setObjss(alist);
        }else{
            companyMessageResp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            companyMessageResp.setReturnMsg("查询公司信息列表调用接口返回失败");
            companyMessageResp.setTimestamp(companyMessageReq.getTimestamp());
        }
    }catch(PwtzDatabaseException e){
        companyMessageResp.setReturnCode(ReturnCode.DB_EXCEPTION);
        companyMessageResp.setReturnMsg("数据库操作异常");
        companyMessageResp.setTimestamp(companyMessageReq.getTimestamp());
    }catch(Exception e){
        companyMessageResp.setReturnCode(ReturnCode.OTHER_ERROR);
        companyMessageResp.setReturnMsg("系统错误");
        companyMessageReq.setTimestamp(companyMessageResp.getTimestamp());
    }
    return companyMessageResp;
}
}
